package com.hyb.algorithm.data.struct.list;

/**
 * @Author: huyanbing
 * @Date: 2021/8/14 7:25 下午
 * <p>
 * 1,2,3,4,5,6,7
 * 1
 */
public class FindElem {

    public static void main(String[] args) {
        //head = [3,2,0,-4], pos = 1

        ListNode head = new ListNode(3);
        head.next = new ListNode(2);
        head.next.next = new ListNode(0);
        head.next.next.next = new ListNode(-4);


        FindElem exe = new FindElem();
        System.out.println(exe.findElem(head, 1).val);
    }

    public ListNode findElem(ListNode head, int k) {

        if (head == null) {
            return null;
        }

        ListNode slow = head;
        ListNode fast = head;
        while (k-- > 0) {
            fast = fast.next;
        }


        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }

        return slow;

    }
}
